define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'travel/service/index' + location.search,
                    add_url: 'travel/service/add',
                    edit_url: 'travel/service/edit',
                    del_url: 'travel/service/del',
                    multi_url: 'travel/service/multi',
                    import_url: 'travel/service/import',
                    table: 'travel_service',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $("#baidu_trans").on('click', function (e) {
                    //来源语言
                    var from_lang=Config.default_lang;
                    //获取当前的语言
                    var lang=$("input[name='row[lang]']:checked").val();
                    if(from_lang==lang){
                        Toastr.error(__('要翻译的语言和来源语音一致'));
                        return false;
                    }
                    //获取需要翻译的值
                    var trans_fields=[{field:'title',type:'input',value:''}];
                    trans_fields.forEach(function (item){
                        if(item.type=="summernote"){
                            item.ftype="textarea";
                        }else{
                            item.ftype=item.type;
                        }
                        if(from_lang=='zh_cn'){
                            item.name=item.ftype+"[name='row["+item.field+"]']";
                        }else{
                            item.name=item.ftype+"[name='row[lang_data]["+from_lang+"]["+item.field+"]']";
                        }
                        item.value=$(item.name).val();
                    });
                    console.log(lang);
                    Toastr.success('翻译中...');
                    $.ajax({
                        url: 'sum/baidu_trans/dotrans',
                        dataType: 'json',
                        data: {lang: lang,fields:JSON.stringify(trans_fields)},
                        cache: false,
                        success: function (ret) {
                            var ret_fields=ret.data.fields;
                            ret_fields.forEach(function (item){
                                if(lang=='zh_cn'){
                                    item.tname=item.ftype+"[name='row["+item.field+"]']";
                                }else{
                                    item.tname=item.ftype+"[name='row[lang_data]["+lang+"]["+item.field+"]']";
                                }
                                if(item.type=='input'){
                                    $(item.tname).val(item.value);
                                }else if(item.type=='textarea'){
                                    $(item.tname).html(item.value);
                                }else if(item.type=='summernote'){
                                    $(item.tname).summernote('pasteHTML', item.value);
                                }
                            });
                            Toastr.success('翻译成功');
                        }, error: function () {
                            Toastr.error(__('翻译失败'));
                        }
                    });
                });
            }
        }
    };
    return Controller;
});
